package com.yanqu.road.server.manger.system;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.servercenter.ChannelStatus;
import com.yanqu.road.logic.bussiness.servercenter.SystemSwitchBusiness;
import com.yanqu.road.pb.login.LoginProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Map;

public class SystemSwitchMgr extends TempMgr {

    private static List<Integer> lockSystemIdList;

    private static LoginProto.SystemLockMsg.Builder lockSystemMsg;


    private static Map<Integer, ChannelStatus> channelStatusMap;
    public static List<Integer> getLockSystemIdList() {
        return lockSystemIdList;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadData() {
        lockSystemMsg = LoginProto.SystemLockMsg.newBuilder();
        try {
            boolean kowloonBefore = lockSystemIdList != null && lockSystemIdList.contains(eSystemId.Kowloon.getValue());
            initLockSystemMsg();
            boolean kowloonAfter = lockSystemIdList != null && lockSystemIdList.contains(eSystemId.Kowloon.getValue());
            initChannelStatusMsg();

            // 是否需要刷新门客技能
            boolean needFlushPatronsSkill = false;
            if (kowloonBefore) {
                // 九龙原先关
                if (kowloonAfter) {
                    // 关，无需处理
                } else {
                    // 开，需要刷新技能
                    needFlushPatronsSkill = true;
                }
            }
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                player.sendPacket(Protocol.U_SYSTEM_LOCK_SYNC, SystemSwitchMgr.getLockSystemMsg());
                if (needFlushPatronsSkill) {
                    player.getModule(PatronsModule.class).flushPatronsSkill();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private void initLockSystemMsg()throws Exception {
        lockSystemIdList = SystemSwitchBusiness.getLockSystemIdList(GameServer.getInstance().getServerId());
        lockSystemMsg.addAllLockSystemIdList(lockSystemIdList);
    }

    private void initChannelStatusMsg() {
        ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
        channelStatusMap = SystemSwitchBusiness.getChannelStatusMap(serverInfo.getChannelId());
        channelStatusMap.forEach((k, v) -> {
            LoginProto.ChannelStatusTemp.Builder builder = LoginProto.ChannelStatusTemp.newBuilder();
            builder.setChannelId(v.getChannelId());
            builder.setSystemId(v.getSystemId());
            builder.setStatus(v.getStatus());
            lockSystemMsg.addChannelStatusList(builder);
        });
    }


    public static LoginProto.SystemLockMsg.Builder getLockSystemMsg() {
        return lockSystemMsg;
    }

    public static Map<Integer, ChannelStatus> getChannelStatusMap() {
        return channelStatusMap;
    }

    public static void setChannelStatusMap(Map<Integer, ChannelStatus> channelStatusMap) {
        SystemSwitchMgr.channelStatusMap = channelStatusMap;
    }
}
